﻿namespace ApophysisCS
{
    #region Imported Namespaces
    using System;
    using System.Diagnostics;
    using System.Globalization;
    using System.Reflection;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
    #endregion

    public class FormattedToolTipSlider : Slider
    {
        #region Members
        private ToolTip autoToolTip;
        #endregion

        #region Accessors
        public string AutoToolTipFormat { get; set; }
        #endregion

        #region Base Overrides
        protected override void OnThumbDragStarted(DragStartedEventArgs e)
        {
            base.OnThumbDragStarted(e);
            this.FormatAutoToolTipContent();
        }

        protected override void OnThumbDragDelta(DragDeltaEventArgs e)
        {
            base.OnThumbDragDelta(e);
            this.FormatAutoToolTipContent();
        }
        #endregion

        #region Private Helpers
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private ToolTip AutoToolTip
        {
            get
            {
                if (this.autoToolTip == null)
                {
                    FieldInfo field = typeof(Slider).GetField("_autoToolTip", BindingFlags.NonPublic | BindingFlags.Instance);
                    this.autoToolTip = field.GetValue(this) as ToolTip;
                }

                return this.autoToolTip;
            }
        }

        private void FormatAutoToolTipContent()
        {
            if (!string.IsNullOrEmpty(this.AutoToolTipFormat))
            {
                this.AutoToolTip.Content = string.Format(
                    CultureInfo.CurrentCulture, 
                    this.AutoToolTipFormat,
                    this.AutoToolTip.Content);
            }
        }
        #endregion
    }
}